<?php
/**
 * Created by PhpStorm.
 * User: poptser@foxmail.com
 * Date: 2018/2/1
 * Time: 10:48
 */

namespace App\Services\Orders;

//自动分配订单到各个配送合作商
use App\Repositories\DistributionCompay\CompanyAreaRepository;
use App\Repositories\Order\OrderInfoRepositoryEloquent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class AutoDispatchOrderService{

    protected $orderService;
    protected $companyArea;
    protected $orderInfo;
    public function __construct(OrderService $orderService,
                                CompanyAreaRepository $companyArea,
                                OrderInfoRepositoryEloquent $orderInfo)
    {
        $this->orderService = $orderService;
        $this->companyArea = $companyArea;
        $this->orderInfo = $orderInfo;
    }


    /**
     * @param int $id
     * @return mixed
     */
    public function getCompanyByArea(int $id){
        return $this->companyArea->findWhere(['area_id'=>$id]);
    }

    /**
     * 自动分派订单到配送合作商
     * order_id 与 order_info  二选其一 order_info 中需包括 order_id wdr_id
     * poptser
     * @param null $order_id
     * @param null $order_info
     * @return bool
     */
    public function dispatchOrder($order_id=null, $order_info =null){
        if(!empty($order_id)){
            $order_area = $this->orderService->getOrderById($order_id,['wdr_id']);
            if(empty($order_area)){
                return false;   //未找到此订单/此订单不是配送单
            }
            $order_info['order_id'] = $order_id;
            $order_info['wdr_id'] = $order_area;
        }

        $company_list =$this->getCompanyByArea($order_info['wdr_id']);
        //获取此区域所有配送商
        if(empty($company_list)){
            return false;   //无关联配送商，不处理
        }
        //查询今日本区域订单
        $order_num = $this->orderInfo->where([
            'order_status'        =>  ['neq',2],
            'pay_status' =>  ['in',2,3],
            'shipping_id'    =>  3,
            'wdr_id'    =>  $order_info['wdr_id'],
            'TO_DAYS(pay_time+28800)'=>TO_DAYS(date("Y-m-d"))
        ])->count('order_id');
        $order_now = $order_num%10;
        $dis_list = array();
        $start_num = 0;
        $dis_company_id = 0;
        foreach ($company_list as $value){
            unset($one_array);
            if($value['order_ratio']==10){
                //此区域全部订单分配到此配送商
                $dis_company_id = $value['company_id'];
                break;
            }else{
                for ($i=0;$i<$value['order_ratio'];$i++){
                    $one_array[] = $i+$start_num;
                }
                $start_num = $i;
                $one_data['id'] = $value['company_id'];
                $one_data['data'] = $one_array;
                $dis_list[] = $one_data;
            }
        }
        if(empty($dis_company_id)){
            foreach ($dis_list as $key=>$value){
                if(in_array($order_now,$value['data'])){
                    $dis_company_id = $value['id'];
                    break;
                }
            }
        }
        if(!empty($dis_company_id)){

            try{
                return $this->orderInfo->update(
                    ['distribution_company_id'=>$dis_company_id],
                    $order_info['order_id']
                );
            }catch (\Exception $exception) {
                //throw new NotFoundHttpException('');
                return false;
            }
        }
    }
}